package com.railway.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.railway.bean.Suggestions;
import com.railway.service.SuggestionsService;
import com.railway.service.impl.SuggestionsServiceImpl;

/**
 *建议
 *
 */
@WebServlet("/suggestions.do")
public class SuggestionsServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String username = req.getParameter("username");//姓名
		String userphone = req.getParameter("userphone");//联系方式
		String userIdType = req.getParameter("userIdType");//证件类型
		String userIdNum = req.getParameter("userIdNum");//证件号
		String suggestionObject = req.getParameter("suggestionObject");//建议对象
		String suggestionText = req.getParameter("suggestionText");//建议内容
		if(suggestionText == null) {
			suggestionText ="";
		}else {
			suggestionText = req.getParameter("suggestionText");
		}
		SuggestionsService sug = new SuggestionsServiceImpl();
		Suggestions sugs = new Suggestions(username,userIdType,userIdNum,userphone,suggestionObject,suggestionText);
		//System.out.println(sugs.toString());
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/json");// 后端告知前端 返回的数据 是 json格式(不是html /css)
		int num = sug.addSuggestions(sugs);
		ObjectMapper om = new ObjectMapper(); 
		resp.getWriter().print(om.writeValueAsString(sugs));
	}
	
}
